package POJO;

import org.jickr.*;
import java.io.*;
import DB.*;

/**
 *
 * @author kristof
 */
public class flickrAPI {

    public flickrAPI() {
    }
    ;
    String key = "7da404de2332062b0ed3f320997db313";
    String secret = "533ddb5c9055707f";

    public boolean upload(String filename, File image, int containerID) throws Exception {

        Flickr.setApiKey(key);
        Flickr.setSharedSecret(secret);
        Permission perm = Permission.WRITE;
        String output;

        org.jickr.User user = Auth.getDefaultAuthUser();
        if (user != null && Auth.isAuthenticated(user, perm)) {
            Auth.setDefaultAuthUser(user);

            Auth.setAuthContext(Auth.getDefaultAuthUser());
        }
        /*
        String authURL = Auth.getAuthURL(perm);
        FileOutputStream out = new FileOutputStream("myfile.txt");
        
        // Connect print stream to the output stream
        PrintStream p = new PrintStream( out );
        
        p.println ("Voer deze url in: "  + authURL);
        Thread.currentThread().sleep(120000);
        
        user = Auth.authenticate();
        Auth.isAuthenticated(user,perm);
        Auth.setDefaultAuthUser(user);
        Auth.setAuthContext(user);*/




        //return output;
        //First, we'll get a photo, and use it to make a file.
        //System.out.println(filename);

        // Now, we'll upload the file
        try {
            Photo newPhoto = Photo.create(image, filename);
            ContentManager cm = new ContentManager(DBInfo.getConnection());
            if (cm.addImage(newPhoto.getStaticURL().toString(), containerID)) {
                return true;
            } else {
                return false;
            }
        } catch (FlickrException ex) {
            return false;
        }
    }
}
